<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2020/4/12
 * Time: 19:45
 */

namespace app\api\controller;

use app\admin\model\Dept;
use app\admin\model\Emplist as EmplistModel;

class Emplist extends Controller
{
    public function index()
    {
        $this->getUser();
        $model = new EmplistModel();
        $emps = $model->getList($this->request->param());
//        $emps = $model->with('dept')->paginate(10);
        return $this->renderSuccess($emps, '获取员工数据成功');
    }

    public function detail($id)
    {
        $model = new EmplistModel();
        $emp = $model->with('dept')->where(['Eno' => $id])->find();
        return $this->renderSuccess($emp, '获取员工数据成功');
    }

    public function depts()
    {
        $depts = Dept::getList();
        return $this->renderSuccess($depts, '获取部门数据成功');
    }

    public function add()
    {
        $model = new EmplistModel();
        if ($model->save($this->request->param())) {
            $new_emp = $model->with('dept')->where(['Eno' => $model->getLastInsID()])->find();
            return $this->renderSuccess($new_emp, '添加成功');
        } else {
            return $this->renderError('添加失败');
        }
    }

    public function edit()
    {
        $model = new EmplistModel();
        if ($model->update($this->request->param())) {
            $new_emp = $model->with('dept')->where(['Eno' => $this->request->param()['Eno']])->find();
            return $this->renderSuccess($new_emp, '修改成功');
        } else {
            return $this->renderError('修改失败');
        }
    }

    public function del($no)
    {
        if (EmplistModel::destroy($no)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

}
